package sg.edu.nus.iss.billing.vo.transaction;

import java.util.Date;

import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.InvalidParamsException;
import sg.edu.nus.iss.billing.vo.common.IDbSerialisable;


public class TransactionVO implements IDbSerialisable{
	
	private String subscriptionId;
	private String numberCalled;
	private double callDuration;
	private Date transactionDate;	
	private String featureName;
	
	public TransactionVO() {
		setSubscriptionId(Util.Default_NULL);
		setFeatureName(Util.Default_NULL);
		setTransactionDate(Util.getNullDate());
		setNumberCalled(Util.Default_NULL);
		setCallDuration(0);
	}
	
	public String getNumberCalled() {
		return numberCalled;
	}
	
	public void setNumberCalled(String numberCalled) {
		this.numberCalled = numberCalled;
	}
	
	public Date getTransactionDate() {
		return transactionDate;
	}
	
	public void setTransactionDate(Date transactionDate) {
		this.transactionDate = transactionDate;
	}

	public double getCallDuration() {
		return callDuration;
	}

	public void setCallDuration(double callDuration) {
		this.callDuration = callDuration;
	}

	/**
	 * @return the subscriptionId
	 */
	public String getSubscriptionId() {
		return subscriptionId;
	}

	/**
	 * @param subscriptionId the subscriptionId to set
	 */
	public void setSubscriptionId(String subscriptionId) {
		this.subscriptionId = subscriptionId;
	}

	public String getFeatureName() {
		return featureName;
	}

	public void setFeatureName(String featureName) {
		this.featureName = featureName;
	}
	
	public void setParameters(String serialisedEntry)
			throws InvalidParamsException {
		String[] tokens =  serialisedEntry.split(java.util.regex.Pattern.quote(DbUtil.DELIMITER));
		if (tokens.length != 5) {
			throw new InvalidParamsException("Expected 5 parameters, received " + tokens.length);
		} else {
			setSubscriptionId(tokens[0]);
			setFeatureName(tokens[1]);
			setNumberCalled(tokens[2]);
			setTransactionDate(Util.getDateFromString(tokens[3]));
			setCallDuration(Double.parseDouble(tokens[4]));
		}
	}

	public String serialised() {
		StringBuilder sb = new StringBuilder();
		sb.append(getSubscriptionId());
		sb.append(DbUtil.DELIMITER);
		sb.append(getFeatureName());
		sb.append(DbUtil.DELIMITER);
		sb.append(getNumberCalled());
		sb.append(DbUtil.DELIMITER);
		sb.append(Util.getFormattedDate(getTransactionDate()));
		sb.append(DbUtil.DELIMITER);
		sb.append(getCallDuration());
		return sb.toString();
	}

}
